﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using DAO;
using BUS;
using DTO;
namespace ProductManagement
{
    public partial class frmNapGas : Form
    {
        DataTable dtGas;
        DataTable dtLoaiGas;
        public frmNapGas()
        {
            InitializeComponent();
            dtGas = new DataTable();
            dtGas.Columns.Add(new DataColumn("STT", typeof(int)));
            dtGas.Columns.Add(new DataColumn("MaGas", typeof(int)));
            dtGas.Columns.Add(new DataColumn("LoaiGas", typeof(String)));
            dtGas.Columns.Add(new DataColumn("SoLuong", typeof(int)));
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                if (cboLoaiGas.SelectedIndex >= 0)
                {

                    int soLuong = int.Parse(txtSoLuong.Value.ToString());
                    int soVoTon = 0;
                    foreach (DataRow aRow in dtLoaiGas.Rows)
                    {
                        if ((int)aRow["MaVo"] == (int)cboLoaiGas.SelectedValue)
                        {
                            soVoTon = int.Parse(aRow["SoLuong"].ToString());
                        }
                    }
                    int maGas = (int)cboLoaiGas.SelectedValue;
                    DataRow curRow = null;
                    foreach (DataRow aRow in dtGas.Rows)
                    {
                        if ((int)aRow["MaGas"] == (int)maGas)
                        {
                            soLuong += (int)aRow["SoLuong"];
                            curRow = aRow;
                            break;
                        }
                    }
                    if (soVoTon < soLuong)
                    {
                        MessageBox.Show("Không đủ vỏ " + GetTenGas(cboLoaiGas.Text) + " để nạp", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        if (curRow == null)
                        {
                            DataRow newRow = dtGas.NewRow();
                            newRow["STT"] = dtGas.Rows.Count + 1;
                            newRow["MaGas"] = maGas;
                            newRow["SoLuong"] = soLuong;
                            newRow["LoaiGas"] = GetTenGas(cboLoaiGas.Text);
                            dtGas.Rows.Add(newRow);
                        }
                        else
                            curRow["SoLuong"] = soLuong;
                        btnCapNhat.Enabled = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void frmNapGas_Load(object sender, EventArgs e)
        {
            try
            {
                String strSql = "select vo.LoaiGas as MaVo, TenLoai &' ('&sum(vo.SoLuong)&')' as LoaiGas, sum(vo.SoLuong) as SoLuong from VoBinhGas vo, LoaiGas loai where vo.LoaiGas=loai.ID group by vo.LoaiGas, loai.TenLoai";
                dtLoaiGas = DataAccess.Query(strSql);
                cboLoaiGas.ValueMember = "MaVo";
                cboLoaiGas.DisplayMember = "LoaiGas";
                cboLoaiGas.DataSource = dtLoaiGas;
                dgvGas.AutoGenerateColumns = false;
                dgvGas.DataSource = dtGas;
                for (int i = 0; i < dgvGas.Rows.Count; i++)
                {
                    dgvGas.Rows[i].Cells["STT"].Value = i + 1;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không thể load form\r\n" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        private String GetTenGas(String str)
        {
            int i1 = str.LastIndexOf('(');
            if (i1 >= 0)
            {
                int i2 = str.LastIndexOf(')');
                return str.Substring(0, i1 - 1);
            }
            return str;
        }
        private void btnXoa_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow aRow in dgvGas.SelectedRows)
            {
                dtGas.Rows.RemoveAt(aRow.Cells[0].RowIndex);
            }
            if (dtGas.Rows.Count == 0)
                btnCapNhat.Enabled = false;
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (DataRow aRow in dtGas.Rows)
                {
                    BinhGasBUS.NapGas((int)aRow["MaGas"], (int)aRow["SoLuong"]);
                }
                MessageBox.Show("Cập nhật dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cập nhật dữ liệu không thành công\r\n" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void dgvGas_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvGas.SelectedRows.Count > 0)
                btnXoa.Enabled = true;
            else
                btnXoa.Enabled = false;
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
